<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2019/3/12
 * Time: 20:39
 */

namespace app\admin\model;


use think\Model;
use think\Request;
use util\Tree;

class AdminMenu extends Model {
    public $data;

    public $menuArr = [];

    protected $location;

    public function __construct($data = []) {
        parent::__construct($data);
        $config = [
            'id' => 'menu_id',
            'pid' => 'parent_id',
            'title' => 'menu_name'
        ];
        Tree::config($config);
    }

    function getLocation(){
        $data = $this->data;
        $curr_id = '';
        $request = Request::instance();
        foreach ($data as $key => $value){
            if($value['controller'] == strtolower($request->controller()) && $value['action'] == $request->action()){
                $curr_id = $value['menu_id'];
            }
        }

        // 获取节点ID是所有父级节点
        $location = Tree::getParents($data, $curr_id);
        return $location;
    }

    function getMenuData($menuIds){
        if($menuIds == 'all'){
            $data = $this->where(['status' => 1])->select()->toArray();
        } else {
            $data = $this->where(['status' => 1])->where('menu_id', 'in', $menuIds)->select()->toArray();
        }
        $this->data = $data;
        if(!empty($data)){
            $location = array_column($this->getLocation(), 'menu_id');
            $this->menuArr = $this->formatData($data, 0, $location);
        }
    }

    function formatData($data, $pid, $selected = [], $level = 0){
        $return = [];
        foreach ($data as $key => $item){
            if($item['parent_id'] == $pid){
                $item['level'] = $level;
                $item['active'] = in_array($item['menu_id'], $selected) ? 'active' : '';
                $item['url'] = url("admin/{$item['controller']}/{$item['action']}");
                $child = $this->formatData($data, $item['menu_id'], $selected, $level + 1);
                if($child){
                    $item['child'] = $child;
                }
                $return[] = $item;
            }
        }
        return $return;
    }

    function getMenuHtml($data){
        $html = '';
        if(!is_array($data)){
            return $html;
        }
        foreach ($data as $key => $item) {
            if(isset($item['child'])){
                $child = $this->getMenuHtml($item['child']);
                $html .= "<li class='{$item['active']} treeview'>
                              <a href='{$item['url']}'>
                              <i class='fa fa-dashboard'></i> <span>{$item['menu_name']}</span>
                                  <span class='pull-right-container'>
                                       <i class='fa fa-angle-left pull-right'></i>
                                  </span>
                               </a>
                              <ul class='treeview-menu side-menu-level-".$item['level']."'>
                                  $child
                              </ul>
                          </li>";
            } /*else if(!$item['parent_id']){
                $html .= "<li class='{$item['active']}'>
                              <a href='{$item['url']}'>
                                    <i class='fa fa-dashboard'></i> <span>{$item['menu_name']}</span>
                               </a>
                          </li>";
            } */else {
                $html .= "<li class='{$item['active']}'>
                                <a href='{$item['url']}'>
                                      <i class='fa fa-circle-o'></i>{$item['menu_name']}
                                  </a>
                           </li>";
            }

        }
        return $html;
    }
}